Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,//用户信息
        is_follow: 0,//是否关注公众号
        tab: 1,//1外卖，2团购
        active: 1,
        isMore: true,
        isLoading: false,
        curPage: 1,
        pageSize: 20,
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.getUserInfo();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.getFollow();
            this.setData({
                userInfo: userInfo
            })
            this.setData({
                tab: 1,
                active: 1,
                isMore: true,
                isLoading: false,
                curPage: 1,
                list: []
            })
            this.getList();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let {curPage,isMore,isLoading} = this.data;
        if(isMore && !isLoading){
            this.setData({
                curPage: curPage+1
            })
            this.getList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    
    
    //切换大标签
    changeTab: function(e){
        let {tab} = this.data;
        let n = e.currentTarget.dataset.type;
        if(n == tab){
            return;
        }
        this.setData({
            tab: n,
            active: 1,
            isLoading: false,
            isMore: true,
            curPage: 1,
            list: []
        })
        this.getList();
    },
    //切换标签
    onChange: function(e){
        let val = e.detail.name;
        let {active} = this.data;
        if(active != val){
            this.setData({
                isLoading: false,
                isMore: true,
                curPage: 1,
                list: [],
                active: val
            })
            this.getList();
        }
    },
    //获取列表
    getList: function(){
        let {tab,active,curPage,pageSize,list} = this.data;
        this.setData({isLoading: true})
        wx.$http({
            url: '/business/order-list',
            method: 'get',
            data: {
                order_type: tab,
                order_status: active,
                page: curPage,
                page_size: pageSize
            },
        }).then((res) => {
            let arr = res.data.data;
            arr.forEach(item => {
                item.pay_time_cn = wx.$moment(Number(item.pay_time)).format('YYYY-MM-DD hh:mm');
                item.cancel_time_cn = wx.$moment(Number(item.cancel_time)).format('YYYY-MM-DD hh:mm');
                item.get_time_cn = wx.$moment(Number(item.get_time)).format('YYYY-MM-DD hh:mm');
                item.catch_time_cn = wx.$moment(Number(item.catch_time)).format('YYYY-MM-DD hh:mm');
                item.end_time_cn = wx.$moment(Number(item.end_time)).format('YYYY-MM-DD hh:mm');
            });
            this.setData({
                isMore: arr.length>=pageSize?true:false,
                list: list.concat(arr)
            })
        }).finally(()=>{
            this.setData({isLoading: false})
        })
    },
    //去外卖详情页面
    goTakeInfo: function(e) {
        let obj = e.currentTarget.dataset.obj;
        wx.navigateTo({
            url: `/pages/takeOut/orderInfo/index?id=${obj.id}`,
        })
    },
    //去团购详情
    goGroupInfo: function(e) {
        let obj = e.currentTarget.dataset.obj;
        wx.navigateTo({
            url: `/pages/groupBuy/orderInfo/index?id=${obj.id}`,
        })
    },

    //通知权限
    openTipsAuthLNoti: function(){
        wx.showModal({
            title: '请开启通知权限',
            content: '可在右上角...>设置>通知管理中设置',
            showCancel: false,
            confirmText: '我知道了'
        })
    },
    

    //登录
    goLogin: function() {
        wx.redirectTo({
          url: '/pages/login/index',
        })
    },

    //公众号
    goGZH: function(){
        wx.redirectTo({
            url: '/pages/webview/index',
        })
    },

    //获取是否关注公众号
    getFollow: function(){
        wx.$http({
            load: true,
            url: '/business/is_follow',
            method: 'get',
            data: {},
        }).then((res) => {
            let data = res.data;
            this.setData({
                is_follow: data.is_follow
            })
        })
    },
    //获取用户信息
    getUserInfo: function() {
        wx.$http({
            load: true,
            url: '/business/get-info',
            method: 'get',
            data: {}
        }).then((res) => {
            if(res.code == 1){
                let data = res.data;
                this.setData({
                    userInfo: data
                })
                wx.setStorageSync('userInfo',data);
            }
        })
    },
})
